package com.example.springstudy.entity;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.realm.SimpleAccountRealm;
import org.apache.shiro.subject.PrincipalCollection;

/**自定义的用于shiro 中realm的类 对数据进行连接的通道*/
public class UsersRealm extends AuthorizingRealm {
    /**用于执行授权逻辑*/
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        System.out.println("用于执行授权逻辑");

        /*对授权的操作*/

        SimpleAuthorizationInfo simple = new SimpleAuthorizationInfo();
        /*授权字符串，就是在拦截的位置定义的字符串*/
        simple.addStringPermission("user:add");
        return simple;
    }
    /**用于执行认证逻辑*/
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        System.out.println("用于执行认证逻辑");
        String userName = "system";
        String passport = "123456";
        /*对用户名进行判断*/
        UsernamePasswordToken usernamePasswordToken = (UsernamePasswordToken) authenticationToken;
        if (!userName.equals(usernamePasswordToken.getUsername())) {
           /*shiro底层会抛出UnKnowAccountException,从而登录失败*/
            return null;
        }
        /*判断密码，自动对password进行比对*/
        return new SimpleAuthenticationInfo("", passport, "");
    }
}
